package pers.lolicer.redtidelibrary

import AppColor
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.WindowInsetsControllerCompat
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import pers.lolicer.redtidelibrary.config.routeConfig
import pers.lolicer.redtidelibrary.pages.MainPage
import pers.lolicer.redtidelibrary.pages.SettingsPage
import pers.lolicer.redtidelibrary.ui.theme.RedTideLibraryTheme

// DataStore
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "setting")

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val context = this.baseContext

        // 设置状态栏与导航栏颜色
        window.statusBarColor = AppColor.Background(context).toArgb()
        WindowInsetsControllerCompat(window, window.decorView).apply {
            // 设置状态栏字体颜色
            isAppearanceLightStatusBars = !AppColor.isSystemInDarkTheme(context) // true 为黑色字体，false 为白色字体
        }
        window.navigationBarColor = AppColor.SystemNavigationBar(context).toArgb()

        setContent {
            RedTideLibraryTheme {
                val navController = rememberNavController()
                NavHost(
                    navController = navController,
                    startDestination = routeConfig.PAGE_MAIN
                ){
                    composable(
                        route = routeConfig.PAGE_MAIN,
                        enterTransition = {slideInHorizontally(
                            animationSpec = tween(200),
                            initialOffsetX = {-it})
                        },
                        exitTransition = {slideOutHorizontally(
                            animationSpec = tween(200),
                            targetOffsetX = {-it})
                        }
                    ){
                        MainPage(navController)
                    }
                    composable(
                        route = routeConfig.PAGE_SETTINGS,
                        enterTransition = {slideInHorizontally(
                                animationSpec = tween(200),
                                initialOffsetX = {it * 2})
                        },
                        exitTransition = {slideOutHorizontally(
                            animationSpec = tween(200),
                            targetOffsetX = {it * 2})
                        }
                    ) {
                        SettingsPage(navController)
                    }
                }
            }
        }
    }
}